﻿using System;
using AudioStudio;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.Liberty
{
    public class AirWallWarning : MonoBehaviour
    {
       public Transform WarningEffect;

       private void OnEnable()
       {
           if (WarningEffect.gameObject.activeSelf)
           {
               WarningEffect.gameObject.SetActive(false);
           }
           Vector3 globalScale = transform.lossyScale;
           WarningEffect.localScale = new Vector3(1f/globalScale.x, 1f/globalScale.y, 1f/globalScale.z);
       }
       
       void OnDestroy()
       {
           hideWarningEffect(null);
       }

       void OnTriggerEnter(Collider collider)
       {
           if (collider.CompareTag("Player"))
           {
               showWarningEffect(collider.transform.position);
           }
       }
       
       void showWarningEffect(Vector3 playerPos)
       {
           Timers.inst.Remove(hideWarningEffect);
           hideWarningEffect(null);

           Vector3 localPos = transform.InverseTransformPoint(playerPos);

           Vector3 weLocalPos = WarningEffect.localPosition;
           WarningEffect.localPosition = new Vector3(localPos.x, 0.97f, weLocalPos.z);
            
           WarningEffect.gameObject.SetActive(false);
           WarningEffect.gameObject.SetActive(true);
           AudioManager.PlaySound("am_street_edgetip");

           Timers.inst.Add(1f, 1, hideWarningEffect);
       }
       void hideWarningEffect(object o)
       {
           WarningEffect.gameObject.SetActive(false);
           AudioManager.StopSound("am_street_edgetip");
       }

    }
}